# Addition
2 + 2
# Subtraction
5.432 - 34234
# Multiplication
33 * 42
# Division
3 / 42
# Modulo (Remainder)
2 %% 2
# Power
2^2
# Combine operations
((2 + 2) * 5)^(10 %% 10)Day 1 - Introduction to Data Analysis with R
Freie Universität Berlin - Theoretical Ecology
October 2, 2023
radius <- 5
There are 6 so-called atomic data types in R. The 4 most important are:
Numeric: There are two numeric data types:
Double: can be specified in decimal (1.243 or -0.2134), scientific notation (2.32e4) or hexadecimal (0xd3f1)
Integer: numbers that are not represented by fraction. Must be followed by an L (1L, 2038459L, -5L)
Logical: only two possible values TRUE and FALSE (abbreviation: T or F - but better use non-abbreviated form)
Character: also called string. Sequence of characters surrounded by quotes ("hello" , "sample_1")
Vectors are data structures that are built on top of atomic data types.
Imagine a vector as a collection of values that are all of the same data type.
c()Use the function c() to combine values into a vector
c()Be aware of implicit type conversion when combining vectors of different types
[1] 1 45 234 1 1 0
[1] "1" "45" "234" "These are" "just"
[6] "some strings"
[1] "TRUE" "TRUE" "FALSE" "These are" "just"
[6] "some strings"
: and seq()The : operator creates a sequence between two numbers with an increment of (-)1
The seq() function creates a sequence of values
[1] 1 2 3 4 5 6 7 8 9 10
[1] 1.000000 1.473684 1.947368 2.421053 2.894737 3.368421 3.842105
[8] 4.315789 4.789474 5.263158 5.736842 6.210526 6.684211 7.157895
[15] 7.631579 8.105263 8.578947 9.052632 9.526316 10.000000
rep()Repeat values multiple times with rep()
Let’s create some vectors to work with.
# list of 10 biggest cities in Europe
cities <- c("Istanbul", "Moscow", "London", "Saint Petersburg", "Berlin",
"Madrid", "Kyiv", "Rome", "Bucharest", "Paris")
population <- c(15.1e6, 12.5e6, 9e6, 5.4e6, 3.8e6, 3.2e6, 3e6, 2.8e6, 2.2e6, 2.1e6)
area_km2 <- c(2576, 2561, 1572, 1439,891,604, 839, 1285, 228, 105 )Divide the population and area vector to calculate the population density in each city:
[1] 5861.801 4880.906 5725.191 3752.606 4264.871 5298.013 3575.685
[8] 2178.988 9649.123 20000.000
The operation is performed separately for each element of the two vectors and the result is a vector.
Same, if a vector is divided by vector of length 1 (i.e. a single number). Result is always a vector.
We can also work with relational and logical operators
The result is a vector containing TRUE and FALSE, depending on whether the city’s population is larger than the mean population or not.
Check whether elements occur in a vector:
The %in% operator checks whether multiple elements occur in a vector.
%in% always returns a vector of the same length as the vector on the left side
You can use square brackets [] to access specific elements from a vector.
The basic structure is:
vector [ vector of indexes to select]
Change the values of a vector at specified indexes using the assignment operator <-
Imagine for example, that the population of
You can also index a vector using logical tests. The basic structure is:
vector [ logical vector of same length]
We also use %in%for logical indexing.
cities returns TRUE for the comparison with "Stockholm"Introduction to R
<-, e.g."hello")23L) and double (2.23)TRUE and FALSE)# By index
v[3]
v[1:4]
v[c(1,5,7)]
# Logical indexing with 1 vector
v[v > 5]
v[v != "bird" | v == "rabbit"]
v[v %in% c(1,2,3)] # same as v[v == 1 | v == 2 | v == 3]
# Logical indexing with two vectors of same length
v[y == "bird"] # return the value in v for which index y == "bird"
v[y == max(y)] # return the value in v for which y is the maximum of yTask 2 (35 min)
Data types and vectors
Find the task description here
Selina Baldauf // Introduction R
Comments in R
#is a commentCtrl/Cmd + Shift + R)